// 用户活动监测JavaScript
window.lockScreenInterop = {
    dotNetObjectRef: null,
    isLocked: false,
    
    initialize: function (dotNetObjectRef) {
        this.dotNetObjectRef = dotNetObjectRef;
        
        // 监听用户活动
        document.addEventListener('mousemove', this.handleUserActivity.bind(this));
        document.addEventListener('keydown', this.handleUserActivity.bind(this));
        document.addEventListener('click', this.handleUserActivity.bind(this));
        document.addEventListener('scroll', this.handleUserActivity.bind(this));
        document.addEventListener('touchstart', this.handleUserActivity.bind(this));
        
        // 监听按键事件，拦截F5、Ctrl+F5、F12
        document.addEventListener('keydown', this.handleKeyDown.bind(this));
    },
    
    handleUserActivity: function () {
        if (this.dotNetObjectRef) {
            this.dotNetObjectRef.invokeMethodAsync('OnUserActivity');
        }
    },
    
    handleKeyDown: function (event) {
        // 如果处于锁屏状态，拦截特定按键
        if (this.isLocked) {
            // 检查是否在密码输入框中按Enter键 - 允许Enter键在输入框中正常工作
            const isPasswordInput = event.target.tagName === 'INPUT' && event.target.type === 'password';
            const isEnterKey = event.key === 'Enter' || event.keyCode === 13;
            
            if (isPasswordInput && isEnterKey) {
                // 允许Enter键在密码输入框中正常工作，不进行任何拦截
                return;
            }
            
            // 拦截F5
            if (event.key === 'F5' || event.keyCode === 116) {
                event.preventDefault();
                event.stopPropagation();
                return;
            }
            
            // 拦截Ctrl+F5 (强制刷新)
            if (event.key === 'F5' && event.ctrlKey) {
                event.preventDefault();
                event.stopPropagation();
                return;
            }
            
            // 拦截Ctrl+R (刷新)
            if (event.key === 'r' && event.ctrlKey) {
                event.preventDefault();
                event.stopPropagation();
                return;
            }
            
            // 拦截F12 (开发者工具)
            if (event.key === 'F12' || event.keyCode === 123) {
                event.preventDefault();
                event.stopPropagation();
                return;
            }
            
            // 拦截Ctrl+Shift+I (开发者工具)
            if (event.key === 'I' && event.ctrlKey && event.shiftKey) {
                event.preventDefault();
                event.stopPropagation();
                return;
            }
            
            // 拦截Ctrl+Shift+J (开发者工具)
            if (event.key === 'J' && event.ctrlKey && event.shiftKey) {
                event.preventDefault();
                event.stopPropagation();
                return;
            }
            
            // 拦截Ctrl+Shift+C (开发者工具)
            if (event.key === 'C' && event.ctrlKey && event.shiftKey) {
                event.preventDefault();
                event.stopPropagation();
                return;
            }
        }
    },
    
    // 设置锁屏状态
    setLockState: function (locked) {
        this.isLocked = locked;
        
        // 在锁屏状态下，禁用右键菜单（防止通过右键刷新）
        if (locked) {
            document.addEventListener('contextmenu', this.disableContextMenu);
        } else {
            document.removeEventListener('contextmenu', this.disableContextMenu);
        }
    },
    
    // 禁用右键菜单
    disableContextMenu: function (event) {
        event.preventDefault();
        return false;
    },
    
    dispose: function () {
        // 清理事件监听
        document.removeEventListener('mousemove', this.handleUserActivity);
        document.removeEventListener('keydown', this.handleUserActivity);
        document.removeEventListener('click', this.handleUserActivity);
        document.removeEventListener('scroll', this.handleUserActivity);
        document.removeEventListener('touchstart', this.handleUserActivity);
        document.removeEventListener('keydown', this.handleKeyDown);
        document.removeEventListener('contextmenu', this.disableContextMenu);
        
        this.dotNetObjectRef = null;
        this.isLocked = false;
    }
};